package ubi.quando.UI;

import ubi.quando.App.EventBase;
import ubi.quando.App.EventType;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class DialogActivity extends Activity {
	public static final int FRIEND_REQUEST_DIALOG = 1;

	private UbiApplication mApp;
	private String mFriendId;
	private String mFriendName;
	private String mNotification;

	private AlertDialog alert;
	private String mEventId;
	private int mEventType;
	private String mPickUpLoc;
	private String mPickUpLocName;
	private long mPickUpTime;
	private EventBase mEvent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mApp = (UbiApplication) getApplication();

		Bundle b = getIntent().getExtras();
		if (b != null) {
			int type = b.getInt("Type");
			
			switch(type) {
			case 1: FriendRequestDialog(b); break;
			case 2: EventInvitationDialog(b); break;
			case 3: NotificationDialog(b); break;
			}
		}
	}

	private void EventInvitationDialog(Bundle b) {

		mEventId = (String) b.getString("EventId");
		mEventType = b.getInt("EventType");
		mEvent = (EventBase) b.getSerializable(EventBase.SerializeName);

		if (mEventType == EventType.ToInt(EventType.I_PICK_U)) {
			mPickUpLoc = b.getString("PickUpLocation");
			mPickUpLocName = b.getString("locName");
			mPickUpTime = b.getLong("PickUpTime");
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(/*mApp
				.getCurrentActivity()*/this);
		builder.setMessage(
				"You have been invited to: " + mEvent.GetName() + " event")
				.setCancelable(false).setPositiveButton("Accept",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								if (mApp.getServerComm().confirmation(mEventId,
										mApp.getId(), mEventType, mPickUpLoc,
										mPickUpTime, mPickUpLocName)) {
									Toast.makeText(mApp.getCurrentActivity(),
											"You have new event",
											Toast.LENGTH_SHORT);
									mApp.addEvent(mEvent);
								}
								getBackToPrevActivity();
							}
						}).setNegativeButton("Reject",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								getBackToPrevActivity();
							}
						});
//		alert = builder.create();
//		alert.show();
		builder.show();
	}

	private void FriendRequestDialog(Bundle b) {
		mFriendId = (String) b.getString("Id");
		mFriendName = b.getString("Name");

		AlertDialog.Builder builder = new AlertDialog.Builder(/*mApp
				.getCurrentActivity()*/this);
		builder.setMessage(
				"Would you like to add " + mFriendName + " to your contacts?")
				.setCancelable(false).setPositiveButton("Accept",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								if (mApp.getServerComm().addContact(mFriendId)) {
									Toast.makeText(mApp.getCurrentActivity(),
											"You have new contact",
											Toast.LENGTH_SHORT);
									mApp.addContact(mApp.getServerComm()
											.getContact(mFriendId));
								}
								getBackToPrevActivity();
							}
						}).setNegativeButton("Reject",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								getBackToPrevActivity();
							}
						});
//		alert = builder.create();
//		alert.show();
		builder.show();
	}

	private void NotificationDialog(Bundle b) {
		mNotification = b.getString("Notification");
		
		AlertDialog.Builder builder = new AlertDialog.Builder(/*mApp
				.getCurrentActivity()*/this);

		builder.setMessage(mNotification).setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						getBackToPrevActivity();
					}
				});
//		alert = builder.create();
//		alert.show();
		builder.show();
	}

	@Override
	protected void onStart() {

//		if (alert != null && !(alert.isShowing())) {
//			alert.show();
//		}
		// TODO Auto-generated method stub
		super.onStart();
	}

	private void getBackToPrevActivity() {
		Intent run = new Intent(getApplicationContext(), MainMenu.class);
		Bundle b = new Bundle();

		run.putExtras(b);
		startActivity(run);
	}
}
